package de.velvettear.squirrel.util;

import java.io.File;

import android.util.Log;
import de.velvettear.squirrel.kernel.Statics;

public class RootTools {

	public static boolean isRooted() {
		Log.i("isRooted()", "BEGIN");
		
		String buildInfo = android.os.Build.TAGS;
		if (buildInfo != null && buildInfo.contains("test-keys")) {
			Log.i("isRooted()", "DEVICE IS ROOTED");
			return true;
		}
		
		try {
			File superuser = new File("/system/app/Superuser.apk");
			if (superuser.exists()) {
				Log.i("isRooted()", "DEVICE IS ROOTED");
				return true;
			}
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("isRooted()", e.getMessage());
			}
		}
		
		for (String rootCommand : Statics.ROOT_COMMANDS) {
			try {
				Runtime.getRuntime().exec(rootCommand);
				Log.i("isRooted()", "DEVICE IS ROOTED");
				return true;
			} catch (Exception e) {
				if (e.getMessage() != null) {
					Log.e("isRooted()", e.getMessage());
				}
			}
		}
		Log.i("isRooted()", "DEVICE IS NOT ROOTED");
		Log.i("isRooted()", "END");
		return false;
	}
	
}
